package net.lab1024.sa.base.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtilsLocal {

    // 常用日期时间格式
    public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_DATE = "yyyy-MM-dd";

    // 获取当前时间字符串
    public static String getCurrentDateTimeStr() {
        return formatLocalDateTime(LocalDateTime.now(), PATTERN_DEFAULT);
    }

    // LocalDateTime 转字符串
    public static String formatLocalDateTime(LocalDateTime dateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }
    public static String formatLocalDateTime(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }

    // 字符串转 LocalDateTime
    public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateTimeStr, formatter);
    }

    // LocalDateTime 转 Date（兼容旧系统）
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    // Date 转 LocalDateTime
    public static LocalDateTime toLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    // 计算两个 LocalDateTime 相差天数
    public static long diffDays(LocalDateTime start, LocalDateTime end) {
        return Duration.between(start, end).toDays();
    }

    // 判断是否为今天
    public static boolean isToday(LocalDateTime dateTime) {
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime startOfDay = dateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        return startOfDay.equals(today);
    }
}